#include "fr_device_charge.h"
#include "app_lvgl.h"


static uint8_t charge_status = CHARGING_IDLE;


/************************************************************************************
 * @fn      charge init
 *
 * @brief   charge init
 */
void device_charge_init(void)
{
    uint32_t timeout_count = 10;
    
    /* enable charge function, and set charge current & voltage. */
    pmu_charge_enable(PMU_CHG_CUR_204mA, PMU_CHG_END_VOL_4_1);
    
    #if 1
    while(timeout_count)
    {
        /* Check that the charger is connected */
        if (read_analog_status() & PMU_CHG_ACOK_STATUS_BIT)
        {   /* filter */
            //system_delay_us(100);
            
            if (read_analog_status() & PMU_CHG_ACOK_STATUS_BIT)
            {   /* The charger is connected. Check out action */
                pmu_charge_monitor_en(PMU_CHARGING_OUT);

                /* Check that the battery is full */
                pmu_battery_full_monitor_en(PMU_BATTERY_FULL);
                printf("charge config 1 \r\n");

                charge_status = CHARGING_IN;
                charge_vbat_toggle_detected(charge_status);

                break;
            }
        }
        else
        {   /* filter */
            //system_delay_us(100);
            if ((read_analog_status() & PMU_CHG_ACOK_STATUS_BIT) == 0)
            {   /* The charger not connected. Check in action */
                pmu_charge_monitor_en(PMU_CHARGING_IN);
				
                printf("charge config 2 \r\n");

                charge_status = CHARGING_IDLE;
                charge_vbat_toggle_detected(charge_status);                
                break;
            }
        }
        
        timeout_count--;
        if (timeout_count == 0)
            printf("ACOK status timeout");
    }

    NVIC_EnableIRQ(PMU_IRQn);
    #endif
    //while(1);
}

void PMU_Charge_Monitor_IRQHandler(void)
{
    if (read_analog_status() & PMU_CHG_ACOK_STATUS_BIT)
    {
        //printf("charge in...\r\n");
        
        /* The charger is connected. Check out action */
        pmu_charge_monitor_en(PMU_CHARGING_OUT);
        
        /* Check that the battery is full */
        pmu_battery_full_monitor_en(PMU_BATTERY_FULL);

        charge_status = CHARGING_IN;
    }
    else
    {
        //printf("charge out...\r\n");
        
        /* The charger not connected. Check in action */
        pmu_charge_monitor_en(PMU_CHARGING_IN);

        charge_status = CHARGING_OUT;
    }

    charge_vbat_toggle_detected(charge_status);
}

void PMU_Battery_Full_IRQHandler(void)
{
    if (read_analog_status() & PMU_BATFULL_STATUS_BIT)
    {
        //printf("full...\r\n");
        
        /* The battery is full. Check that the battery not full */
        pmu_battery_full_monitor_en(PMU_BATTERY_NOT_FULL);

        charge_status = CHARGING_FULL;
    }
    else
    {
        //printf("not full\r\n");

        /* The battery not full. Check that the battery is full */
        pmu_battery_full_monitor_en(PMU_BATTERY_FULL);

        charge_status = CHARGING_NOT_FULL;
    }

    charge_vbat_toggle_detected(charge_status);
}


/*
    send vbat charge status
*/
void charge_vbat_toggle_detected(uint8_t sat)
{
	struct app_task_event *event;
	event = app_task_event_alloc(APP_TASK_EVENT_CHARGE_OUTPUT, sizeof(uint8_t), false);
	if(event) 
	{
		memcpy(event->param, (void *)&sat, sizeof(uint8_t));
		event->param_len = sizeof(uint8_t);
		app_task_event_post(event, false);
	}
}


void charge_event_handle(uint8_t param)
{
    printf("%s param=%d \r\n",__func__,param);
    switch(param)
    {
        case CHARGING_IDLE:
        printf("---> CHARGING_IDLE \r\n");
        break;

        case CHARGING_IN:
        printf("---> CHARGING_IN \r\n");
        if(gui_task_handle_is_active() == NULL)
            return;
        gui_task_resume();
        gui_task_msg_send(POWER_CHARGE_IN_EVT,NULL,0,NULL,0,NULL);
        break;

        case CHARGING_OUT:
        printf("---> CHARGING_OUT \r\n");
        if(gui_task_handle_is_active() == NULL)
            return;
        gui_task_resume();
        gui_task_msg_send(POWER_CHARGE_OUT_EVT,NULL,0,NULL,0,NULL);
        break;

        case CHARGING_FULL:
        printf("---> CHARGING_FULL \r\n");
        break;

        case CHARGING_NOT_FULL:
        printf("---> CHARGING_NOT_FULL \r\n");
        break;        
    }
}


uint8_t get_charge_status(void)
{
    return charge_status;
}














